/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    usart.c
  * @brief   This file provides code for the configuration
  *          of the USART instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"

/* USER CODE BEGIN 0 */
void usart1_send(uint8_t data)
{
	USART1->DR = data;
	while((USART1->SR&0x40)==0);	
}
extern u8 place_information[6];
extern u8 store_information[6];
uint8_t Uart1_Receive_buf[1];          //串口1接收中断数据存放的缓冲区
uint8_t Uart2_Receive_buf[1]; 
char u4copy[UART4_RX_BUF_SIZE];
char u5copy[UART5_RX_BUF_SIZE];
// 添加新的全局变量
char UART4_RX_BUF[UART4_RX_BUF_SIZE];
uint16_t UART4_RX_STA = 0;
uint8_t Uart4_Receive_buf[1];
char UART5_RX_BUF[UART5_RX_BUF_SIZE];
uint16_t UART5_RX_STA = 0;
uint8_t Uart5_Receive_buf[1]; 
/* USER CODE END 0 */

UART_HandleTypeDef huart4;
UART_HandleTypeDef huart5;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;

/* UART4 init function */
void MX_UART4_Init(void)
{

  /* USER CODE BEGIN UART4_Init 0 */

  /* USER CODE END UART4_Init 0 */

  /* USER CODE BEGIN UART4_Init 1 */

  /* USER CODE END UART4_Init 1 */
  huart4.Instance = UART4;
  huart4.Init.BaudRate = 115200;
  huart4.Init.WordLength = UART_WORDLENGTH_8B;
  huart4.Init.StopBits = UART_STOPBITS_1;
  huart4.Init.Parity = UART_PARITY_NONE;
  huart4.Init.Mode = UART_MODE_TX_RX;
  huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart4) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN UART4_Init 2 */
    HAL_UART_Receive_IT(&huart4, Uart4_Receive_buf, sizeof(Uart4_Receive_buf));
  /* USER CODE END UART4_Init 2 */

}
/* UART5 init function */
void MX_UART5_Init(void)
{

  /* USER CODE BEGIN UART5_Init 0 */

  /* USER CODE END UART5_Init 0 */

  /* USER CODE BEGIN UART5_Init 1 */

  /* USER CODE END UART5_Init 1 */
  huart5.Instance = UART5;
  huart5.Init.BaudRate = 115200;
  huart5.Init.WordLength = UART_WORDLENGTH_8B;
  huart5.Init.StopBits = UART_STOPBITS_1;
  huart5.Init.Parity = UART_PARITY_NONE;
  huart5.Init.Mode = UART_MODE_TX_RX;
  huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart5.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart5) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN UART5_Init 2 */
    HAL_UART_Receive_IT(&huart5, Uart5_Receive_buf, sizeof(Uart5_Receive_buf));
  /* USER CODE END UART5_Init 2 */

}
/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 230400;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */
   HAL_UART_Receive_IT(&huart1,Uart1_Receive_buf,sizeof(Uart1_Receive_buf));
  /* USER CODE END USART1_Init 2 */

}
/* USART2 init function */

void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 230400;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */
   HAL_UART_Receive_IT(&huart2,Uart2_Receive_buf,sizeof(Uart2_Receive_buf));
  /* USER CODE END USART2_Init 2 */

}
/* USART3 init function */

void MX_USART3_UART_Init(void)
{

  /* USER CODE BEGIN USART3_Init 0 */

  /* USER CODE END USART3_Init 0 */

  /* USER CODE BEGIN USART3_Init 1 */

  /* USER CODE END USART3_Init 1 */
  huart3.Instance = USART3;
  huart3.Init.BaudRate = 115200;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART3_Init 2 */

  /* USER CODE END USART3_Init 2 */

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==UART4)
  {
  /* USER CODE BEGIN UART4_MspInit 0 */

  /* USER CODE END UART4_MspInit 0 */
    /* UART4 clock enable */
    __HAL_RCC_UART4_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    /**UART4 GPIO Configuration
    PC10     ------> UART4_TX
    PC11     ------> UART4_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    /* UART4 interrupt Init */
    HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(UART4_IRQn);
  /* USER CODE BEGIN UART4_MspInit 1 */

  /* USER CODE END UART4_MspInit 1 */
  }
  else if(uartHandle->Instance==UART5)
  {
  /* USER CODE BEGIN UART5_MspInit 0 */

  /* USER CODE END UART5_MspInit 0 */
    /* UART5 clock enable */
    __HAL_RCC_UART5_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOD_CLK_ENABLE();
    /**UART5 GPIO Configuration
    PC12     ------> UART5_TX
    PD2     ------> UART5_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    /* UART5 interrupt Init */
    HAL_NVIC_SetPriority(UART5_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(UART5_IRQn);
  /* USER CODE BEGIN UART5_MspInit 1 */

  /* USER CODE END UART5_MspInit 1 */
  }
  else if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspInit 0 */

  /* USER CODE END USART2_MspInit 0 */
    /* USART2 clock enable */
    __HAL_RCC_USART2_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART2 interrupt Init */
    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspInit 1 */

  /* USER CODE END USART2_MspInit 1 */
  }
  else if(uartHandle->Instance==USART3)
  {
  /* USER CODE BEGIN USART3_MspInit 0 */

  /* USER CODE END USART3_MspInit 0 */
    /* USART3 clock enable */
    __HAL_RCC_USART3_CLK_ENABLE();

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**USART3 GPIO Configuration
    PB10     ------> USART3_TX
    PB11     ------> USART3_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* USART3 interrupt Init */
    HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART3_IRQn);
  /* USER CODE BEGIN USART3_MspInit 1 */

  /* USER CODE END USART3_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==UART4)
  {
  /* USER CODE BEGIN UART4_MspDeInit 0 */

  /* USER CODE END UART4_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_UART4_CLK_DISABLE();

    /**UART4 GPIO Configuration
    PC10     ------> UART4_TX
    PC11     ------> UART4_RX
    */
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11);

    /* UART4 interrupt Deinit */
    HAL_NVIC_DisableIRQ(UART4_IRQn);
  /* USER CODE BEGIN UART4_MspDeInit 1 */

  /* USER CODE END UART4_MspDeInit 1 */
  }
  else if(uartHandle->Instance==UART5)
  {
  /* USER CODE BEGIN UART5_MspDeInit 0 */

  /* USER CODE END UART5_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_UART5_CLK_DISABLE();

    /**UART5 GPIO Configuration
    PC12     ------> UART5_TX
    PD2     ------> UART5_RX
    */
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_12);

    HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);

    /* UART5 interrupt Deinit */
    HAL_NVIC_DisableIRQ(UART5_IRQn);
  /* USER CODE BEGIN UART5_MspDeInit 1 */

  /* USER CODE END UART5_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspDeInit 0 */

  /* USER CODE END USART2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART2_CLK_DISABLE();

    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

    /* USART2 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspDeInit 1 */

  /* USER CODE END USART2_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART3)
  {
  /* USER CODE BEGIN USART3_MspDeInit 0 */

  /* USER CODE END USART3_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART3_CLK_DISABLE();

    /**USART3 GPIO Configuration
    PB10     ------> USART3_TX
    PB11     ------> USART3_RX
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);

    /* USART3 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART3_IRQn);
  /* USER CODE BEGIN USART3_MspDeInit 1 */

  /* USER CODE END USART3_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */
char USART_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.
uint16_t point1 ;
LidarPointTypedef Pack_Data[12];/* 雷达接收的数据储存在这个变量之中 */
LidarPointTypedef Pack_Data_y[12];
LidarPointTypedef Pack_sum;/* 输出结果储存 */
LidarPointTypedef Pack_sum_y;
extern uint16_t receive_cnt,receive_cnt_y;;
extern uint8_t confidence,confidence_y;
extern uint16_t distance,distance_y,noise,noise_y,reftof,reftof_y;
extern uint32_t peak,peak_y,intg,intg_y;
uint16_t i_usart=0;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) //接收回调函数
{

	static uint8_t state = 0;			//状态位	
	static uint8_t crc = 0;				//校验和
	static uint8_t cnt = 0;				//用于一帧12个点的计数
	static uint8_t PACK_FLAG = 0;  //命令标志位
	static uint8_t data_len  = 0;  //数据长度
	static uint32_t timestamp = 0; //时间戳
	static uint8_t state_flag = 1; //转入数据接收标志位
	uint8_t temp_data;
	static uint8_t state_y = 0;			//状态位	
	static uint8_t crc_y = 0;				//校验和
	static uint8_t cnt_y = 0;				//用于一帧12个点的计数
	static uint8_t PACK_FLAG_y = 0;  //命令标志位
	static uint8_t data_len_y  = 0;  //数据长度
	static uint32_t timestamp_y = 0; //时间戳
	static uint8_t state_flag_y = 1; //转入数据接收标志位
	uint8_t temp_data_y;
	if(huart -> Instance == USART1)
	{
			temp_data=Uart1_Receive_buf[0];	
			if(state< 4) 																					 /* 起始符验证 前4个数据均为0xAA */
				{                                          
						if(temp_data == HEADER) state ++;
						else state = 0;
				}
				else if(state<10&&state>3)
				{
						switch(state)
						{
								case 4:   
									if(temp_data == device_address)              /* 设备地址验证 */
									{							
													state ++;
													crc = crc + temp_data;									
													break;        
									} 
									else state = 0,crc = 0;
								case 5:   
									if(temp_data == PACK_GET_DISTANCE)					 /* 获取测量数据命令 */
									{  
													PACK_FLAG = PACK_GET_DISTANCE;
													state ++;	
													crc = crc + temp_data;	
													break;									
									}		 

									else if(temp_data == PACK_RESET_SYSTEM) 		 /* 复位命令 */
									{
													PACK_FLAG = PACK_RESET_SYSTEM;
													state ++; 
													crc = crc + temp_data;	
													break;	
									}
									else if(temp_data == PACK_STOP)							 /* 停止测量数据传输命令 */
									{ 
													PACK_FLAG = PACK_STOP;
													state ++; 
													crc = crc + temp_data;	
													break;
									}
									else if(temp_data == PACK_ACK)							 /* 应答码命令 */
									{  
													PACK_FLAG = PACK_ACK;
													state ++;
													crc = crc + temp_data;	
													break;
									}			 				 
									else if(temp_data == PACK_VERSION)					 /* 获取传感器信息命令 */
									{ 
													PACK_FLAG = PACK_VERSION,
													state ++,
													crc = crc + temp_data;	   	     
													break;
									}
									else state = 0,crc = 0;
								case 6: if(temp_data == chunk_offset)          /* 偏移地址 */
												{  
													state ++;
													crc = crc + temp_data;
													break; 	  
												}	
												else state = 0,crc = 0;
								case 7: if(temp_data == chunk_offset)
												{  
													state ++;
													crc = crc + temp_data;
													break;
												}
												else state = 0,crc = 0;
								case 8: 
										data_len = (u16)temp_data;								 /* 数据长度低八位 */
										state ++; 
										crc = crc + temp_data;
										break;																			 
								case 9: 
										data_len = data_len + ((u16)temp_data<<8); 			 /* 数据长度高八位 */
										state ++;
										crc = crc + temp_data;
										break; 
								default: break;
						}
				}
				else if(state == 10 ) state_flag = 0;                    /*由switch跳出来时state为10，但temp_data仍为距离长度高八位数据，需跳过一次中断*/
				if(PACK_FLAG == PACK_GET_DISTANCE&&state_flag == 0)      /* 获取一帧数据并校验 */
				{
						if(state>9)
						{
								if(state<190)
								{
										static uint8_t state_num;
										state_num = (state-10)%15;
										switch(state_num)
										{
												case 0: 
													Pack_Data[cnt].distance = (uint16_t)temp_data ;				 /* 距离数据低八位 */
													crc = crc + temp_data;
													state++;
													break;        
												case 1: 
													Pack_Data[cnt].distance = ((u16)temp_data<<8) + Pack_Data[cnt].distance;	 /* 距离数据 */
													crc = crc + temp_data;
													state++;
													break; 
												case 2:
													Pack_Data[cnt].noise = (u16)temp_data;				 /* 环境噪音低八位 */
													crc = crc + temp_data;
													state++;
													break; 
												case 3:
													Pack_Data[cnt].noise = ((u16)temp_data<<8) + Pack_Data[cnt].noise;				 /* 环境噪音 */
													crc = crc + temp_data;
													state++;
													break; 
												case 4:
													Pack_Data[cnt].peak = (u32)temp_data;				 										 /* 接受强度信息低八位 */
													crc = crc + temp_data;
													state++;
													break; 
												case 5:
													Pack_Data[cnt].peak = ((u32)temp_data<<8) + Pack_Data[cnt].peak;
													crc = crc + temp_data;
													state++;
													break; 
												case 6:
													Pack_Data[cnt].peak = ((u32)temp_data<<16) + Pack_Data[cnt].peak;	
													crc = crc + temp_data;
													state++;
													break; 
												case 7:
													Pack_Data[cnt].peak = ((u32)temp_data<<24) + Pack_Data[cnt].peak;				    /* 接受强度信息 */
													crc = crc + temp_data;
													state++;
													break; 
												case 8:
													Pack_Data[cnt].confidence = temp_data;				 /* 置信度 */
													crc = crc + temp_data;
													state++;
													break; 
												case 9:
													Pack_Data[cnt].intg = (u32)temp_data;															/* 积分次数低八位 */
													crc = crc + temp_data;
													state++;
													break; 
												case 10:
													Pack_Data[cnt].intg = ((u32)temp_data<<8) + Pack_Data[cnt].intg;
													crc = crc + temp_data;
													state++;
													break; 
												case 11:
													Pack_Data[cnt].intg = ((u32)temp_data<<16) + Pack_Data[cnt].intg;
													crc = crc + temp_data;
													state++;
													break; 
												case 12:
													Pack_Data[cnt].intg = ((u32)temp_data<<24) + Pack_Data[cnt].intg;				  	 /* 积分次数 */
													crc = crc + temp_data;
													state++;
													break; 
												case 13:
													Pack_Data[cnt].reftof = (int16_t)temp_data;				 								 /* 温度表征值低八位 */
													crc = crc + temp_data;
													state++;
													break; 
												case 14:
													Pack_Data[cnt].reftof = ((int16_t)temp_data<<8) +Pack_Data[cnt].reftof;			/* 温度表征值 */
													crc = crc + temp_data;
													state++;
													cnt++;							 /* 进入下一个测量点 */
													break; 
												default: break;
										}
							}
										/* 时间戳 */ //因为第12个距离点结束后，STATE值为190，因此，时间帧应该从191开始，STATE从191开始
										if(state == 191) timestamp = temp_data,state++,crc = crc + temp_data;
										else if(state == 192) timestamp = ((u32)temp_data<<8) + timestamp,state++,crc = crc + temp_data; 
										else if(state == 193) timestamp = ((u32)temp_data<<16) + timestamp,state++,crc = crc + temp_data;
										else if(state == 194) timestamp = ((u32)temp_data<<24) + timestamp,state++,crc = crc + temp_data; 
										else if(state==195)
										{
													if(temp_data == crc)   /* 校验成功 */
													{
															data_process();  	 /* 数据处理函数，完成一帧之后可进行数据处理 */
															receive_cnt++;	 	 /* 输出接收到正确数据的次数 */
													}
													distance = Pack_Data[0].distance;
													crc = 0;
													state = 0;
													state_flag = 1;
													cnt = 0; 							 /* 复位*/
										}
										 if(state == 190) state++;//state等于190的数据实际是第12个测距点的最后一位数据，数据已经取走使用
							
						}
				}
			HAL_UART_Receive_IT(&huart1,Uart1_Receive_buf,sizeof(Uart1_Receive_buf));
	}
	if(huart -> Instance == USART2)
	{
			temp_data_y=Uart2_Receive_buf[0];	
			if(state_y< 4) 																					 /* 起始符验证 前4个数据均为0xAA */
				{                                          
						if(temp_data_y == HEADER) state_y ++;
						else state_y = 0;
				}
				else if(state_y<10&&state_y>3)
				{
						switch(state_y)
						{
								case 4:   
									if(temp_data_y == device_address)              /* 设备地址验证 */
									{							
													state_y ++;
													crc_y = crc_y + temp_data_y;									
													break;        
									} 
									else state_y = 0,crc_y = 0;
								case 5:   
									if(temp_data_y == PACK_GET_DISTANCE)					 /* 获取测量数据命令 */
									{  
													PACK_FLAG_y = PACK_GET_DISTANCE;
													state_y ++;	
													crc_y = crc_y + temp_data_y;	
													break;									
									}		 

									else if(temp_data_y == PACK_RESET_SYSTEM) 		 /* 复位命令 */
									{
													PACK_FLAG_y = PACK_RESET_SYSTEM;
													state_y ++; 
													crc_y = crc_y + temp_data_y;	
													break;	
									}
									else if(temp_data_y == PACK_STOP)							 /* 停止测量数据传输命令 */
									{ 
													PACK_FLAG_y = PACK_STOP;
													state_y ++; 
													crc_y = crc_y + temp_data_y;	
													break;
									}
									else if(temp_data_y == PACK_ACK)							 /* 应答码命令 */
									{  
													PACK_FLAG_y = PACK_ACK;
													state_y ++;
													crc_y = crc_y + temp_data_y;	
													break;
									}			 				 
									else if(temp_data_y == PACK_VERSION)					 /* 获取传感器信息命令 */
									{ 
													PACK_FLAG_y = PACK_VERSION,
													state_y ++,
													crc_y = crc_y + temp_data_y;	   	     
													break;
									}
									else state_y = 0,crc_y = 0;
								case 6: if(temp_data_y == chunk_offset)          /* 偏移地址 */
												{  
													state_y ++;
													crc_y = crc_y + temp_data_y;
													break; 	  
												}	
												else state_y = 0,crc_y = 0;
								case 7: if(temp_data_y == chunk_offset)
												{  
													state_y ++;
													crc_y = crc_y + temp_data_y;
													break;
												}
												else state_y = 0,crc_y = 0;
								case 8: 
										data_len_y = (u16)temp_data_y;								 /* 数据长度低八位 */
										state_y ++; 
										crc_y = crc_y + temp_data_y;
										break;																			 
								case 9: 
										data_len_y = data_len_y + ((u16)temp_data_y<<8); 			 /* 数据长度高八位 */
										state_y ++;
										crc_y = crc_y + temp_data_y;
										break; 
								default: break;
						}
				}
				else if(state_y == 10 ) state_flag_y = 0;                    /*由switch跳出来时state_y为10，但temp_data_y仍为距离长度高八位数据，需跳过一次中断*/
				if(PACK_FLAG_y == PACK_GET_DISTANCE&&state_flag_y == 0)      /* 获取一帧数据并校验 */
				{
						if(state_y>9)
						{
								if(state_y<190)
								{
										static uint8_t state_num_y;
										state_num_y = (state_y-10)%15;
										switch(state_num_y)
										{
												case 0: 
													Pack_Data_y[cnt_y].distance = (uint16_t)temp_data_y ;				 /* 距离数据低八位 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break;        
												case 1: 
													Pack_Data_y[cnt_y].distance = ((u16)temp_data_y<<8) + Pack_Data_y[cnt_y].distance;	 /* 距离数据 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 2:
													Pack_Data_y[cnt_y].noise = (u16)temp_data_y;				 /* 环境噪音低八位 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 3:
													Pack_Data_y[cnt_y].noise = ((u16)temp_data_y<<8) + Pack_Data_y[cnt_y].noise;				 /* 环境噪音 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 4:
													Pack_Data_y[cnt_y].peak = (u32)temp_data_y;				 										 /* 接受强度信息低八位 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 5:
													Pack_Data_y[cnt_y].peak = ((u32)temp_data_y<<8) + Pack_Data_y[cnt_y].peak;
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 6:
													Pack_Data_y[cnt_y].peak = ((u32)temp_data_y<<16) + Pack_Data_y[cnt_y].peak;	
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 7:
													Pack_Data_y[cnt_y].peak = ((u32)temp_data_y<<24) + Pack_Data_y[cnt_y].peak;				    /* 接受强度信息 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 8:
													Pack_Data_y[cnt_y].confidence = temp_data_y;				 /* 置信度 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 9:
													Pack_Data_y[cnt_y].intg = (u32)temp_data_y;															/* 积分次数低八位 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 10:
													Pack_Data_y[cnt_y].intg = ((u32)temp_data_y<<8) + Pack_Data_y[cnt_y].intg;
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 11:
													Pack_Data_y[cnt_y].intg = ((u32)temp_data_y<<16) + Pack_Data_y[cnt_y].intg;
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 12:
													Pack_Data_y[cnt_y].intg = ((u32)temp_data_y<<24) + Pack_Data_y[cnt_y].intg;				  	 /* 积分次数 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 13:
													Pack_Data_y[cnt_y].reftof = (int16_t)temp_data_y;				 								 /* 温度表征值低八位 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													break; 
												case 14:
													Pack_Data_y[cnt_y].reftof = ((int16_t)temp_data_y<<8) +Pack_Data_y[cnt_y].reftof;			/* 温度表征值 */
													crc_y = crc_y + temp_data_y;
													state_y++;
													cnt_y++;							 /* 进入下一个测量点 */
													break; 
												default: break;
										}
							}
										/* 时间戳 */ //因为第12个距离点结束后，STATE值为190，因此，时间帧应该从191开始，STATE从191开始
										if(state_y == 191) timestamp_y = temp_data_y,state_y++,crc_y = crc_y + temp_data_y;
										else if(state_y == 192) timestamp_y = ((u32)temp_data_y<<8) + timestamp_y,state_y++,crc_y = crc_y + temp_data_y; 
										else if(state_y == 193) timestamp_y = ((u32)temp_data_y<<16) + timestamp_y,state_y++,crc_y = crc_y + temp_data_y;
										else if(state_y == 194) timestamp_y = ((u32)temp_data_y<<24) + timestamp_y,state_y++,crc_y = crc_y + temp_data_y; 
										else if(state_y==195)
										{
													if(temp_data_y == crc_y)   /* 校验成功 */
													{
															data_process_y();  	 /* 数据处理函数，完成一帧之后可进行数据处理 */
															receive_cnt_y++;	 	 /* 输出接收到正确数据的次数 */
													}
													distance_y = Pack_Data_y[0].distance;
													crc_y = 0;
													state_y = 0;
													state_flag_y = 1;
													cnt_y = 0; 							 /* 复位*/
										}
										 if(state_y == 190) state_y++;//state_y等于190的数据实际是第12个测距点的最后一位数据，数据已经取走使用
							
						}
				}
			HAL_UART_Receive_IT(&huart2,Uart2_Receive_buf,sizeof(Uart2_Receive_buf));
	}
	if(huart -> Instance == UART4)
	{
		char temp_data = Uart4_Receive_buf[0];
		if(temp_data == '\n')
		{
			if(UART4_RX_STA > 0)
			{
				UART4_RX_BUF[UART4_RX_STA] = '\0'; // 字符串结束符
				UART4_Receive_Process(); // 处理接收到的数据
				memset(UART4_RX_BUF, 0, UART4_RX_BUF_SIZE); // 清空接收缓冲区
				UART4_RX_STA = 0; // 重置接收状态
			}
		}
		else
		{
			if(UART4_RX_STA < UART4_RX_BUF_SIZE - 1)
			{
				UART4_RX_BUF[UART4_RX_STA++] = temp_data;
			}
		}
		HAL_UART_Receive_IT(&huart4, Uart4_Receive_buf, sizeof(Uart4_Receive_buf));
	}
	if(huart -> Instance == UART5)
	{
		char temp_data = Uart5_Receive_buf[0];
		if(temp_data == '\n')
		{
			if(UART5_RX_STA > 0)
			{
				UART5_RX_BUF[UART5_RX_STA] = '\0'; // 字符串结束符
				UART5_Receive_Process(); // 处理接收到的数据
				memset(UART5_RX_BUF, 0, UART5_RX_BUF_SIZE); // 清空接收缓冲区
				UART5_RX_STA = 0; // 重置接收状态
			}
		}
		else
		{
			if(UART5_RX_STA < UART5_RX_BUF_SIZE - 1)
			{
				UART5_RX_BUF[UART5_RX_STA++] = temp_data;
			}
		}
		HAL_UART_Receive_IT(&huart5, Uart5_Receive_buf, sizeof(Uart5_Receive_buf));
	}

}

uint8_t Hand(char *a)                   // 串口命令识别函数
{ 
    if(strstr(USART_RX_BUF,a)!=NULL)
	    return 1;
	else
		return 0;
}

void CLR_Buf(void)                           // 串口缓存清理
{
	memset(USART_RX_BUF, 0, USART_REC_LEN);      //清空
  point1 = 0;                    
}

void data_process(void)/*数据处理函数，完成一帧之后可进行数据处理*/
{
		/* 计算距离 */
		u8 i;
		static u16 count = 0;
		LidarPointTypedef Pack_sum;
		for(i=0;i<12;i++)									/* 12个点取平均 */
		{
				if(Pack_Data[i].distance != 0)  /* 去除0的点 */
				{
						count++;
						Pack_sum.distance += Pack_Data[i].distance;
						Pack_sum.noise += Pack_Data[i].noise;
						Pack_sum.peak += Pack_Data[i].peak;
						Pack_sum.confidence += Pack_Data[i].confidence;
						Pack_sum.intg += Pack_Data[i].intg;
						Pack_sum.reftof += Pack_Data[i].reftof;
				}
		}
		if(count !=0)
		{

					distance = Pack_sum.distance/count;
					noise = Pack_sum.noise/count;
					peak = Pack_sum.peak/count;
					confidence = Pack_sum.confidence/count;
					intg = Pack_sum.intg/count;
					reftof = Pack_sum.reftof/count;
					Pack_sum.distance = 0;
					Pack_sum.noise = 0;
					Pack_sum.peak = 0;
					Pack_sum.confidence = 0;
					Pack_sum.intg = 0;
					Pack_sum.reftof = 0;
					count = 0;
		}
}
void data_process_y(void)/*数据处理函数，完成一帧之后可进行数据处理*/
{
		/* 计算距离 */
		u8 i_y;
		static u16 count_y = 0;
		LidarPointTypedef Pack_sum_y;
		for(i_y=0;i_y<12;i_y++)									/* 12个点取平均 */
		{
				if(Pack_Data_y[i_y].distance != 0)  /* 去除0的点 */
				{
						count_y++;
						Pack_sum_y.distance += Pack_Data_y[i_y].distance;
						Pack_sum_y.noise += Pack_Data_y[i_y].noise;
						Pack_sum_y.peak += Pack_Data_y[i_y].peak;
						Pack_sum_y.confidence += Pack_Data_y[i_y].confidence;
						Pack_sum_y.intg += Pack_Data_y[i_y].intg;
						Pack_sum_y.reftof += Pack_Data_y[i_y].reftof;
				}
		}
		if(count_y !=0)
		{

					distance_y = Pack_sum_y.distance/count_y;
					noise_y = Pack_sum_y.noise/count_y;
					peak_y = Pack_sum_y.peak/count_y;
					confidence_y = Pack_sum_y.confidence/count_y;
					intg_y = Pack_sum_y.intg/count_y;
					reftof_y = Pack_sum_y.reftof/count_y;
					Pack_sum_y.distance = 0;
					Pack_sum_y.noise = 0;
					Pack_sum_y.peak = 0;
					Pack_sum_y.confidence = 0;
					Pack_sum_y.intg = 0;
					Pack_sum_y.reftof = 0;
					count_y = 0;
		}
}
void UART4_Receive_Process(void)
{
	my_data_process(UART4_RX_BUF,place_information,u4copy);	
//    	strcpy(u4copy, UART4_RX_BUF);		
		// OLED_Clear();			
		// OLED_ShowString(1,1,u4copy,OLED_8X16);
		// OLED_ShowString(1,1+16,u5copy,OLED_8X16);
		// OLED_Update();
	
}
void UART5_Receive_Process(void)
{
	my_data_process(UART5_RX_BUF,store_information,u5copy);	
//    	strcpy(u5copy, UART5_RX_BUF);		
 //   	strcpy(u5copy, UART5_RX_BUF);		
		// OLED_Clear();			
		// OLED_ShowString(1,1,u4copy,OLED_8X16);
		// OLED_ShowString(1,1+16,u5copy,OLED_8X16);
		// OLED_Update();
	
}
void UART_ClearRxBuffer(char* buf, uint16_t* sta) {
    memset(buf, 0, UART4_RX_BUF_SIZE); // 根据实际使用的缓冲区大小调整
    *sta = 0;
}
void sendHexData_360(void) {
    uint8_t hexData[] = {0x01, 0xFD, 0x01, 0x03, 0xE8, 0x64, 0x00, 0x00, 
                         0x0C, 0x80, 0x01, 0x00, 0x6B};
    uint16_t dataLength = sizeof(hexData);
    
    // 调用HAL库函数发送数据
    // 参1：串口句柄，参2：数据缓冲区，参3：数据长度，参4：超时时间
    HAL_UART_Transmit(&huart3, hexData, dataLength, 1000);
}
void sendHexData_0(void) {
    uint8_t hexData[] = {0x01, 0xFD, 0x01, 0x03, 0xE8, 0x64, 0x00, 0x00, 
                         0x00, 0x00, 0x01, 0x00, 0x6B};
    uint16_t dataLength = sizeof(hexData);
    
    // 调用HAL库函数发送数据
    // 参1：串口句柄，参2：数据缓冲区，参3：数据长度，参4：超时时间
    HAL_UART_Transmit(&huart3, hexData, dataLength, 1000);
}
void sendHexData_180(void) {
    uint8_t hexData[] = {0x01, 0xFD, 0x01, 0x03, 0xE8, 0x64, 0x00, 0x00, 
                         0x06, 0x40, 0x01, 0x00, 0x6B};
    uint16_t dataLength = sizeof(hexData);
    
    // 调用HAL库函数发送数据
    // 参1：串口句柄，参2：数据缓冲区，参3：数据长度，参4：超时时间
    HAL_UART_Transmit(&huart3, hexData, dataLength, 1000);
}
void sendHexDataP_180(void) {
    uint8_t hexData[] = {0x01, 0xFD, 0x00, 0x03, 0xE8, 0x64, 0x00, 0x00, 
                         0x06, 0x40, 0x01, 0x00, 0x6B};
    uint16_t dataLength = sizeof(hexData);
    
    // 调用HAL库函数发送数据
    // 参1：串口句柄，参2：数据缓冲区，参3：数据长度，参4：超时时间
    HAL_UART_Transmit(&huart3, hexData, dataLength, 1000);
}
/**
 * @brief  发送位置模式控制指令（十进制参数版本）
 * @param  addr:      设备地址（1~255）
 * @param  direction: 旋转方向（0=CW顺时针，1=CCW逆时针）
 * @param  speed_rpm: 速度（RPM，0~65535）
 * @param  accel:     加速度档位（0~255）
 * @param  pulse_cnt: 脉冲数（0~4294967295）
 * @param  pos_mode:  位置模式（0=相对，1=绝对）
 * @param  sync_mode: 多机同步（0=关闭，1=开启）
 */
void SendPositionControlCmd(
    uint8_t addr,
    uint8_t direction,
    uint16_t speed_rpm,
    uint8_t accel,
    uint32_t pulse_cnt,
    uint8_t pos_mode,
    uint8_t sync_mode
) 
{
    uint8_t tx_buf[13] = {0};
    
    // 填充固定字段和参数
    tx_buf[0] = addr;                     // 设备地址
    tx_buf[1] = 0xFD;                     // 命令头
    tx_buf[2] = direction;                // 方向
    
    // 速度（RPM转16位，大端模式）
    tx_buf[3] = (speed_rpm >> 8) & 0xFF;  // 高字节
    tx_buf[4] = speed_rpm & 0xFF;         // 低字节
    
    tx_buf[5] = accel;                    // 加速度档位
    
    // 脉冲数（32位，大端模式）
    tx_buf[6] = (pulse_cnt >> 24) & 0xFF; // 最高字节
    tx_buf[7] = (pulse_cnt >> 16) & 0xFF;
    tx_buf[8] = (pulse_cnt >> 8) & 0xFF;
    tx_buf[9] = pulse_cnt & 0xFF;         // 最低字节
    
    tx_buf[10] = pos_mode;                // 位置模式
    tx_buf[11] = sync_mode;               // 多机同步
    
    // 计算校验和（前12字节的和，取低8位）
    uint8_t checksum = 0;
    for (int i = 0; i < 12; i++) {
        checksum += tx_buf[i];
    }
    tx_buf[12] = checksum;
    
    // 发送数据包
    HAL_UART_Transmit(&huart3, tx_buf, 13, HAL_MAX_DELAY);
}
// void UART4_ConvertToStore(uint8_t store_information[6]) {
//     for (int i = 0; i < 6; i++) {
//         store_information[i] = UART5_RX_BUF[i] - '0';
//     }
// }
// void UART5_ConvertToPlace(uint8_t place_information[6]) {
//     for (int i = 0; i < 6; i++) {
//         place_information[i] = UART4_RX_BUF[i] - '0';
//     }
// }
void my_data_process(char *usartchar,uint8_t informatian[6],char *informatian_str)
{
	for(int i=0;i<6;i++)
	{
		informatian[i] = usartchar[i] - '0'; // 将字符转换为数字
		informatian_str[i] = usartchar[i]; // 将字符复制到字符串中
	}
	informatian_str[7]="\0"; // 添加字符串结束符
} 
/* USER CODE END 1 */
